package com.qifenqian.turbot.module.ui.order.orders

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseFragment
import com.qifenqian.turbot.module.adapter.order.OrderPagerAdapter
import com.qifenqian.turbot.module.contract.OrderContract
import com.qifenqian.turbot.module.presenter.OrderPresenter
import com.qifenqian.turbot.util.Lcat
import kotlinx.android.synthetic.main.fragment_order_orders.view.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * @author H
 * @createDate 2019/1/10 11:54
 * @description 订单订单界面
 * @see
 * @since
 */
class TradeOrderFragment : BaseFragment<OrderPresenter, OrderContract.View>(), OrderContract.View {

    override fun bindLayoutId(): Int {
        return R.layout.fragment_order_orders
    }

    override fun bindPresenter(): OrderPresenter? {
        return OrderPresenter(activity!!, this)
    }

    private var mTitles = mutableListOf<String>("全部", "就餐中", "订座", "完成", "退款")
    private var mTabFragments = mutableListOf<Fragment>()

    private var mOrderPagerAdapter: OrderPagerAdapter? = null
    override fun initial(view: View) {
        Lcat.a("3", "onCreateView")
        mTabFragments.clear()   //因为onCreateView方法会多次走
        for (i in 0..4) {
            mTabFragments.add(TabOrderFragment.newInstance(i))
        }
        mOrderPagerAdapter = OrderPagerAdapter(childFragmentManager, mTitles, mTabFragments)
        view.tl_order_orders.setupWithViewPager(view.vp_order_orders)
        view.vp_order_orders.adapter = mOrderPagerAdapter
    }

    /**这是第三个Fragment(总共3个)
     * 第一次点击旁边的Fragment,或该Fragment的时候 123456788 onAttach onCreate onCreateView onViewCreated onActivityCreated onStart onResume onAttachFragment onAttachFragment(fragment的子fragment)
     * 再点击该Fragment,无变化(即预加载 预加载与实际加载的生命周期一样)
     * 再点第一个Fragment的时候 9 10 11  onPause onStop onDestroyView
     * 再点第二个Fragment的时候 34567 onCreateView onViewCreated onActivityCreated onStart onResume
     *
     * 保持当前Fragment(即未onDestroyView时),离开该Fragment对应的Activity的时候,生命周期与其Activity一样 onPause onStop 恢复则为 onStart onResume
     * 退出当前Activity onPause onStop onDestroyView(如果推出前保持在当前Fragment则包含这三个方法) onDestroy onDetach
     * 总之 onStart onResume onPause onStop这几个方法与对应Activity是一样的
     *
     * 所以onAttach 和 onCreate只走一次
     * 34567 onCreateView onViewCreated onActivityCreated onStart onResume每次加载的时候都会走
     * 每次销毁,离开界面的时候(所谓离开当前界面包含被预加载的情况,即点击当前界面与点击旁边界面是一样的)都会走9 10 11  onPause onStop onDestroyView
     */

    //第一次加载回调
    override fun onAttach(context: Context?) {
        super.onAttach(context)
        Lcat.a("1", "onAttach")
    }

    //第一次加载回调
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Lcat.a("2", "onCreate")
    }

    //每次加载回调
//    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//        Lcat.a("3", "onCreateView")
//        return super.onCreateView(inflater, container, savedInstanceState)
//    }

    //每次加载回调
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Lcat.a("4", "onViewCreated")
    }

    //每次加载回调
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Lcat.a("5", "onActivityCreated")
    }

    //每次加载回调,类似Activity的onStart
    override fun onStart() {
        super.onStart()
        Lcat.a("6", "onStart")
    }

    //每次加载回调,类似Activity的onResume
    override fun onResume() {
        super.onResume()
        Lcat.a("7", "onResume")
    }

    //第一次加载回调
    override fun onAttachFragment(childFragment: Fragment?) {
        super.onAttachFragment(childFragment)
        Lcat.a("8", "onAttachFragment")
    }

    //每次离开回调,类似Activity的onPause
    override fun onPause() {
        super.onPause()
        Lcat.a("9", "onPause")
    }

    //每次离开回调,类似Activity的onStop
    override fun onStop() {
        super.onStop()
        Lcat.a("10", "onStop")
    }

    //每次离开回调
    override fun onDestroyView() {
        super.onDestroyView()
        Lcat.a("11", "onDestroyView")
    }

    //Activity销毁的时候回调
    override fun onDestroy() {
        super.onDestroy()
        Lcat.a("12", "onDestroy")
    }

    //Activity销毁的时候回调
    override fun onDetach() {
        super.onDetach()
        Lcat.a("13", "onDetach")
    }
}
